package com.jltx.xq.simplewatch.utils;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import cn.jltx.helper.FragmentLifecycleCallbacks;
import cn.jltx.support.SupportAppCompatActivity;
import cn.jltx.support.SupportFragment;
import cn.jltx.utils.XqLog;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2017/08/20.
 */

public class LifecycleInstrumentation {
    public static final String TAG = "LifecycleInstrumentation";
    private static FragmentLifecycleCallbacks fragmentLifecycleCallbacks = new FragmentLifecycleCallbacks() {
        // 可以监听该Activity下的所有Fragment的18个 生命周期方法

        @Override
        public void onFragmentCreated(SupportFragment fragment, Bundle savedInstanceState) {
            XqLog.i(TAG, "onFragmentCreated--->" + fragment.getClass().getSimpleName());
        }

        @Override
        public void onFragmentSaveInstanceState(SupportFragment fragment, Bundle outState) {
            super.onFragmentSaveInstanceState(fragment, outState);
            XqLog.i(TAG, "onFragmentSaveInstanceState--->" + fragment.getClass().getSimpleName());
        }

        @Override
        public void onFragmentEnterAnimationEnd(SupportFragment fragment, Bundle savedInstanceState) {
            super.onFragmentEnterAnimationEnd(fragment, savedInstanceState);
            XqLog.i(TAG, "onFragmentEnterAnimationEnd--->" + fragment.getClass().getSimpleName());
        }

        @Override
        public void onFragmentLazyInitView(SupportFragment fragment, Bundle savedInstanceState) {
            super.onFragmentLazyInitView(fragment, savedInstanceState);
            XqLog.i(TAG, "onFragmentLazyInitView--->" + fragment.getClass().getSimpleName());
        }

        @Override
        public void onFragmentSupportVisible(SupportFragment fragment) {
            super.onFragmentSupportVisible(fragment);
            XqLog.i(TAG, "onFragmentSupportVisible--->" + fragment.getClass().getSimpleName());
        }

        @Override
        public void onFragmentSupportInvisible(SupportFragment fragment) {
            super.onFragmentSupportInvisible(fragment);
            XqLog.i(TAG, "onFragmentSupportInvisible--->" + fragment.getClass().getSimpleName());
        }

        @Override
        public void onFragmentAttached(SupportFragment fragment) {
            super.onFragmentAttached(fragment);
            XqLog.i(TAG, "onFragmentAttached--->" + fragment.getClass().getSimpleName());
        }

        @Override
        public void onFragmentStarted(SupportFragment fragment) {
            super.onFragmentStarted(fragment);
            XqLog.i(TAG, "onFragmentStarted--->" + fragment.getClass().getSimpleName());
        }

        @Override
        public void onFragmentResumed(SupportFragment fragment) {
            super.onFragmentResumed(fragment);
            XqLog.i(TAG, "onFragmentResumed--->" + fragment.getClass().getSimpleName());
        }

        @Override
        public void onFragmentPaused(SupportFragment fragment) {
            super.onFragmentPaused(fragment);
            XqLog.i(TAG, "onFragmentPaused--->" + fragment.getClass().getSimpleName());
        }

        @Override
        public void onFragmentStopped(SupportFragment fragment) {
            super.onFragmentStopped(fragment);
            XqLog.i(TAG, "onFragmentStopped--->" + fragment.getClass().getSimpleName());
        }

        @Override
        public void onFragmentDestroyView(SupportFragment fragment) {
            super.onFragmentDestroyView(fragment);
            XqLog.i(TAG, "onFragmentDestroyView--->" + fragment.getClass().getSimpleName());
        }

        @Override
        public void onFragmentDestroyed(SupportFragment fragment) {
            super.onFragmentDestroyed(fragment);
            XqLog.i(TAG, "onFragmentDestroyed--->" + fragment.getClass().getSimpleName());
        }

        @Override
        public void onFragmentDetached(SupportFragment fragment) {
            super.onFragmentDetached(fragment);
            XqLog.i(TAG, "onFragmentDetached--->" + fragment.getClass().getSimpleName());
        }

        @Override
        public void onFragmentActivityCreated(SupportFragment fragment, Bundle savedInstanceState) {
            super.onFragmentActivityCreated(fragment, savedInstanceState);
            XqLog.i(TAG, "onFragmentActivityCreated--->" + fragment.getClass().getSimpleName());
        }

        @Override
        public void onFragmentViewCreated(SupportFragment fragment, Bundle savedInstanceState) {
            super.onFragmentViewCreated(fragment, savedInstanceState);
            XqLog.i(TAG, "onFragmentViewCreated--->" + fragment.getClass().getSimpleName());
        }
    };

    public static Application.ActivityLifecycleCallbacks activityLifecycleCallbacks = new Application.ActivityLifecycleCallbacks(){
        @Override
        public void onActivityCreated(Activity activity, Bundle bundle) {
            XqLog.i(TAG, "onActivityCreated--->" + activity.getClass().getSimpleName());
        }

        @Override
        public void onActivityStarted(Activity activity) {
            XqLog.i(TAG, "onActivityStarted--->" + activity.getClass().getSimpleName());
        }

        @Override
        public void onActivityResumed(Activity activity) {
            XqLog.i(TAG, "onActivityResumed--->" + activity.getClass().getSimpleName());
        }

        @Override
        public void onActivityPaused(Activity activity) {
            XqLog.i(TAG, "onActivityPaused--->" + activity.getClass().getSimpleName());
        }

        @Override
        public void onActivityStopped(Activity activity) {
            XqLog.i(TAG, "onActivityStopped--->" + activity.getClass().getSimpleName());
        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

        }

        @Override
        public void onActivityDestroyed(Activity activity) {
            XqLog.i(TAG, "onActivityDestroyed--->" + activity.getClass().getSimpleName());
        }
    };

    public static void registerFragmentLifecycleCallbacks(SupportAppCompatActivity activity) {
        activity.registerFragmentLifecycleCallbacks(fragmentLifecycleCallbacks);
    }

    public static void unregisterFragmentLifecycleCallbacks(SupportAppCompatActivity activity) {
        activity.unregisterFragmentLifecycleCallbacks(fragmentLifecycleCallbacks);
    }
}
